package com.dataimport.service.impl;

import cn.hutool.cache.Cache;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.ECardFlowRecord;
import com.dataimport.entity.targetentity.ECardConsumeRecord;
import com.dataimport.entity.targetentity.SchoolCalendar;
import com.dataimport.mapper.ECardConsumeRecordMapper;
import com.dataimport.mapper.ECardFlowRecordMapper;
import com.dataimport.mapper.SchoolCalendarMapper;
import com.dataimport.service.ECardConsumeRecordService;
import com.dataimport.service.SchoolCalendarService;
import com.dataimport.utils.HasNullFieldUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 卑微小峰
 * @date 2022/9/27
 */
@Service
public class ECardConsumeRecordServiceImpl extends ServiceImpl<ECardConsumeRecordMapper, ECardConsumeRecord> implements ECardConsumeRecordService {

    private static long PAGE_SIZE = 1000;

    @Autowired
    private ECardFlowRecordMapper eCardFlowRecordMapper;

    @Override
    public void dealWithECardConsumeRecord() {

        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<ECardConsumeRecord> eCardConsumeRecordList = selectData(currentPage);
            if (ObjectUtils.isEmpty(eCardConsumeRecordList)) {
                break;
            }
            insertData(eCardConsumeRecordList);
            System.out.println("ECardConsumeRecord表导入成功");
        }
    }

    public List<ECardConsumeRecord> selectData(long currentPage) {
        DynamicDataSourceContextHolder.push("mysql2");
        IPage<ECardFlowRecord> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(PAGE_SIZE);
        IPage<ECardFlowRecord> data = eCardFlowRecordMapper.selectPage(page, null);
        DynamicDataSourceContextHolder.poll();

        List<ECardFlowRecord> eCardFlowRecordList = data.getRecords();
        List<ECardConsumeRecord> eCardConsumeRecordList = eCardFlowRecordList.stream()
                .filter(eCardFlowRecord -> HasNullFieldUtil.inExistenceNull(eCardFlowRecord))
                .map(eCardFlowRecord -> {
                    ECardConsumeRecord eCardConsumeRecord = new ECardConsumeRecord();
                    BeanUtils.copyProperties(eCardFlowRecord, eCardConsumeRecord);
                    eCardConsumeRecord.setSlotCardTime(DateUtil.parse(eCardFlowRecord.getSksj()));
                    eCardConsumeRecord.setECardBalance(Double.parseDouble(eCardFlowRecord.getKnye()));
                    eCardConsumeRecord.setDealAmount(Double.parseDouble(eCardFlowRecord.getJyje()));
                    eCardConsumeRecord.setConsumeType(eCardFlowRecord.getXFLXDM().equals("311") ? 0 : 1);
                    return eCardConsumeRecord;
                })
                .collect(Collectors.toList());

        return eCardConsumeRecordList;
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertData(List<ECardConsumeRecord> eCardConsumeRecordList) {
        try {
            DynamicDataSourceContextHolder.push("mysql1");
            this.saveBatch(eCardConsumeRecordList, eCardConsumeRecordList.size());
            DynamicDataSourceContextHolder.poll();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
